package com.zz.springboot.controller;

import com.zz.springboot.constant.PathConstants;
import com.zz.springboot.resp.enums.RespCodeEnum;
import com.zz.springboot.exception.BizException;
import com.zz.springboot.resp.RespTemplate;
import com.zz.springboot.service.GenerateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

@RestController
@Slf4j
public class QiaHaoController {

    @Autowired
    GenerateService generateService;

    @Value("${my.name}")
    private String name;

    /**
     * @param originData
     * @desc 适合单类型、单条数据处理
     * @tips 若在调用完文本处理接口后，还需单独处理若干少量数据，建议使用该接口
     */
    @RequestMapping("/parseSingleData/{type}")
    public void parseSingleData(String originData, @PathVariable("type")Integer type) {

        generateService.parseData(originData, type);

    }

    /**
     * @desc 适合一次性处理文本中所有的买码数据
     */
    //@RequiresPermissions("readText")
    @RequestMapping("/readText/{type}")
    public RespTemplate readText(@PathVariable("type") Integer type){

        switch (type) {
            case 1:
                generateService.clearAoMenTable(); //先清空表数据
                pick(type, PathConstants.AOMEN_TXT_PATH);
                break;
            case 2:
                generateService.clearXiangGangTable(); //先清空表数据
                pick(type, PathConstants.XIANG_GANG_TXT_PATH);
                break;
            default:
                throw new BizException(RespCodeEnum.PARAMS_VERIFY_FAIL);
        }

        return RespTemplate.success();
    }

    /**
     * 打印要报出的数据
     *
     * @return
     */
    @RequiresRoles(value = {"member", "admin"}, logical =Logical.OR)
    @RequestMapping("/printExclude/{type}")
    public String printExcludeInfo(@PathVariable("type")Integer type){

        return generateService.printAllExcludeInfo(type);
    }

    public void pick(Integer type, String txtPath) {

        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(txtPath));
            String line = br.readLine();  //这里只读取了第一行
            //不能写成while ((line = br.readLine())!=null && (line = br.readLine())!="")，因为br.readLine()每调用一次读取的就是下一行数据
            while (StringUtils.hasText(line)) {
                log.info("--------开始处理：[{}]" ,line);
                generateService.parseData(line, type);
                //读取下一行，否则会一直会读取第一行并死循环。br.readLine()读到文件末尾时会返回null
                line = br.readLine();
            }
/*         //下面这个方式会读取空行
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println("--------开始处理：" + line);
                generateService.parseData(line);
            }
*/
        } catch (IOException e) {
            log.error("IO异常：", e);
            throw new BizException(RespCodeEnum.IO_EXCEPTION);
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }

   // @RequiresPermissions("func1")
    @RequestMapping("/test1")
    public RespTemplate test1() {
        System.out.println( name);
        log.info("--------test1");
        return RespTemplate.success();
    }

    //@RequiresPermissions("func2")
    @RequestMapping("/test2")
    public RespTemplate test2() {
        log.info("--------test2");
        return RespTemplate.success();
    }

    //@RequiresRoles(value = {"member","admin"},logical = Logical.OR)
    @RequestMapping("/test3")
    public RespTemplate test3() {
        log.info("--------test3");
        return RespTemplate.success();
    }

    //@RequiresRoles("admin")
    @RequestMapping("/test4")
    public RespTemplate test4() {
        log.info("--------test4");
        return RespTemplate.success();
    }

    @RequestMapping("/test5")
    public RespTemplate test5() {
        log.info("--------test5");
        return RespTemplate.success();
    }


}
